<?php
/**
 * Created by PhpStorm.
 * User: jhon
 * Date: 2018/2/9
 * Time: 9:37
 */

namespace app\admin\controller;
use think\Request;
use think\Db;

class Device extends Auto
{

    public function index(){
        $result  = Db::table('organization')->select();
        $this->assign('org_list',$result);
        return $this->fetch();
    }

    public function deviceadd(){
        $Request = Request::instance();
        $param = $Request->param();
        $data = [
            'org_code'=>$param['org_code'],
            'dev_code'=>$param['dev_code'],
            'dev_color'=>$param['dev_color'],
            'dev_status'=>1,
            'dev_mac'=>$param['dev_mac'],
            'dev_name'=>$param['dev_name'],
            'mark'=>1,
            'create_date'=>date('Y-m-d H:i:s',time()),

            'dev_type' => input('dev_type'),
            'dev_edition' => input('dev_edition'),
            'dev_soft_edition' => input('dev_soft_edition'),
        ];
        $result  = Db::table('device')->insert($data);
        if($result){
            return $result;
        }else{//添加失败
            return 0;
        }
    }

    public function devicedel(){
        $Request = Request::instance();
        $param = $Request->param();
        if($param['ID'] == 1){
            return "error";
        }
        $ID = $param['ID'];
        $result  = Db::table('device')->delete($ID);
        echo $result;
    }

    public function deviceupd(){
        $Request = Request::instance();
        $param = $Request->param();
        $dev_sn = $param['dev_sn'];
        $data = [
            'dev_name' => $param['dev_name'],
            'org_code' => $param['org_code'],
            'dev_code' => $param['dev_code'],
            'dev_color' => $param['dev_color'],
            'dev_status' => $param['dev_status'],
            'dev_mac' => $param['dev_mac'],
        ];
//        echo json_encode($data);exit();
        try{
            $result  = Db::table('device')->where('dev_sn',$dev_sn)->update($data);
            echo $result;
        } catch (Exception $e){
//            echo $e->getMessage();
            echo '服务器内部错误';
        };
    }

    public function devicequery(){
        $join = [
            ['organization w','a.org_code=w.org_code'],
        ];
        $result  = Db::table('device')
            ->where('org_code',session('user.org_code'))
          /*  ->alias('a')
            ->join($join)*/
            ->select();
        $dataarr = [
            'data'=>$result
        ];
        echo json_encode($dataarr);
    }

    public function devicesel(){
        $Request = Request::instance();
        $param = $Request->param();
        $ID = $param['ID'];
        $result  = Db::table('device')->where('dev_sn',$ID)->select();
        echo json_encode($result[0]);
    }

//    分配设备归属机构
    public function devdistributionbyorg(){

        $Request = Request::instance();
        $param = $Request->param();
        $dev_sn = $param['dev_sn'];
        $data = [
            'org_code' => $param['org_code'],
        ];

        $result  = Db::table('device')->where('dev_sn',$dev_sn)->update($data);
        echo $result;
//        echo json_encode($result[0]);
    }
}